package com.ruoyi.demo.service.impl;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.mapper.UserdemoMapper;
import com.ruoyi.demo.domain.Userdemo;
import com.ruoyi.demo.service.IUserdemoService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

/**
 * 测试数据隔离Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-12
 */
@Service
public class UserdemoServiceImpl  implements IUserdemoService
{
    @Autowired
    private UserdemoMapper userdemoMapper;

    @Autowired
    private ISysUserService userService;

    public String sessionId;

//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        sessionId = req.getSession().getId();
//
//        super.doGet(req, resp);
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doGet(req,resp);
//    }

    /**
     * 查询测试数据隔离
     *
     * @param id 测试数据隔离主键
     * @return 测试数据隔离
     */

    @Override
    public Userdemo selectUserdemoById(Long id)
    {
        return userdemoMapper.selectUserdemoById(id);
    }

    /**
     * 查询测试数据隔离列表
     *
     * @param userdemo 测试数据隔离
     * @return 测试数据隔离
     */
    @Override
    @DataScope(userAlias = "u",deptAlias = "d")
    public List<Userdemo> selectUserdemoList(Userdemo userdemo)
    {

        return userdemoMapper.selectUserdemoList(userdemo);
    }

    /**
     * 新增测试数据隔离
     *
     * @param userdemo 测试数据隔离
     * @return 结果
     */
    @Override
    public int insertUserdemo(Userdemo userdemo)
    {

        //在添加对应的数据时候，记录对应的用户id喝部门id
        Long userId = SecurityUtils.getUserId();
        userdemo.setUserId(userId);

        //根据用户查询对应的部门id
        SysUser sysUser = userService.selectUserById(userId);
        Long deptId = sysUser.getDeptId();
        userdemo.setDeptId(deptId);

        return userdemoMapper.insertUserdemo(userdemo);
    }

    /**
     * 修改测试数据隔离
     *
     * @param userdemo 测试数据隔离
     * @return 结果
     */
    @Override
    public int updateUserdemo(Userdemo userdemo)
    {
        return userdemoMapper.updateUserdemo(userdemo);
    }

    /**
     * 批量删除测试数据隔离
     *
     * @param ids 需要删除的测试数据隔离主键
     * @return 结果
     */
    @Override
    public int deleteUserdemoByIds(Long[] ids)
    {
        return userdemoMapper.deleteUserdemoByIds(ids);
    }

    /**
     * 删除测试数据隔离信息
     *
     * @param id 测试数据隔离主键
     * @return 结果
     */
    @Override
    public int deleteUserdemoById(Long id)
    {
        return userdemoMapper.deleteUserdemoById(id);
    }
}
